/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */


package com.example.soscall.activity;





import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.lifecycle.ViewModel;

import com.ecarx.common.BaseActivity;
import com.ecarx.common.Constant;
import com.ecarx.xui.adaptapi.oncall.Call;
import com.example.soscall.Constans;
import com.example.soscall.MyApplication;
import com.example.soscall.R;
import com.example.soscall.PhoneLiveData;
import com.example.soscall.bean.WeatherBean;
import com.example.soscall.databinding.ActivityMainBinding;
import com.example.soscall.utils.CallHelper;
import com.example.soscall.utils.CommonUtil;
import com.example.soscall.utils.HttpUtils;
import com.example.soscall.utils.RxUtils;
import com.example.soscall.utils.SeniverseUtils;
import com.example.soscall.utils.WindowUtils;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.disposables.Disposables;
import io.reactivex.observers.DisposableObserver;


public class MainActivity extends BaseActivity<PhoneLiveData, ActivityMainBinding> {
    private static final String TAG = Constant.Companion.getBASE_TAG() + MainActivity.class.getSimpleName();

    /**
     * ECall正常拨号
     */
    private void testNormalCallInNewThread() {
        Constans.CALL_TYPE = Call.CALL_TYPE_ECALL;
        new Thread(new Runnable() {
            @Override
            public void run() {
                CallHelper.getInstance().doTestCallStatusChanged(Call.CALL_TYPE_ECALL, Call.CALL_STATUS_DATA_UPLOADING);
                int[] pairs = new int[]{
                        Call.CALL_STATUS_DATA_UPLOADING,
                        Call.CALL_STATUS_DIALING, Call.CALL_STATUS_OFFHOOK,
                        Call.CALL_STATUS_END
                };
                for (int i = 0; i < pairs.length; i++) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    CallHelper.getInstance().doTestCallStatusChanged(Call.CALL_TYPE_ECALL, pairs[i]);
                }
            }
        }).start();
    }

    /**
     * Bcall的正常拨号
     */
    private void testNormalBCallInNewThread() {
        new Thread(() -> {
            CallHelper.getInstance().doTestCallStatusChanged(Call.CALL_TYPE_BCALL, Call.CALL_STATUS_DATA_UPLOADING);
            int[] pairs = new int[]{
                    Call.CALL_STATUS_DATA_UPLOADING,
                    Call.CALL_STATUS_DIALING, Call.CALL_STATUS_OFFHOOK,
                    Call.CALL_STATUS_END
            };
            for (int i = 0; i < pairs.length; i++) {
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                CallHelper.getInstance().doTestCallStatusChanged(Call.CALL_TYPE_BCALL, pairs[i]);
            }
        }).start();
    }

    private void testFailCallInNewThread() {
//        ConstantBase.CALL_TYPE = Call.CALL_TYPE_ECALL;

        new Thread(() -> {
            CallHelper.getInstance().doTestCallStatusChanged(Call.CALL_TYPE_ECALL, Call.CALL_STATUS_DATA_UPLOADING);
            int[] pairs = new int[]{
                    Call.CALL_STATUS_IN_ECALL_CALLBACK, Call.CALL_STATUS_IN_ECALL_CALLBACK, Call.CALL_STATUS_IN_ECALL_CALLBACK, Call.CALL_STATUS_IN_ECALL_CALLBACK, Call.CALL_STATUS_DIAL_FAILED
            };
            for (int i = 0; i < pairs.length; i++) {
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                CallHelper.getInstance().doTestCallStatusChanged(Call.CALL_TYPE_ECALL, pairs[i]);
            }
        }).start();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    private String responseText;

    @Override
    protected void initView() {
        onClick(binding.tvNormal, (v) -> {
            CallHelper.getInstance().onCallStatusChangedUIThread(1, Call.CALL_STATUS_CONNECTING);
            CallHelper.getInstance().startECall();
//            CallHelper.getInstance().onCallStatusChangedUIThread(1, Call.CALL_STATUS_OFFHOOK);
//            WindowUtils.showCallNormalWindow(1, Call.CALL_STATUS_CONNECTING);
        });
        onClick(binding.tvMini, (v) -> {
            disposable.add(CommonUtil.getWeather("30.200066320416877:120.24846900914",(result, data) -> {
                if(result!=null){
                    Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_SHORT).show();
                }
            }));
        });
//        onClick(binding.tvLittle,(v)->{
//            WindowUtils.mViewType = WindowUtils.ViewType.LITTLE;
//            CallHelper.getInstance().onCallStatusChangedUIThread(1, Call.CALL_STATUS_CONNECTING);
//        });
//        onClick(binding.tvInterative,(v)->{
//            CallHelper.getInstance().onCallStatusChangedUIThread(1, Call.CALL_STATUS_START_FAILED);
//        });

        onClick(binding.tvSos, (v) ->WindowUtils.showSosArrivePopView(this));

        onClick(binding.tvRescueTime, (v) ->

        {
            WindowUtils.showRescueWindow(this, 1, 10);
        });

        onClick(binding.testNormal, (v) ->

        {
            testNormalCallInNewThread();
        });

        onClick(binding.testFailed, (v) ->

        {
            testFailCallInNewThread();
        });

        onClick(binding.btEcall, (v) ->

        {
            Intent intent = new Intent();
            intent.setAction("close");
            intent.setPackage("ecarx.xcgoncall");
            intent.putExtra("type", "6666");
            sendBroadcast(intent);
        });

        onClick(binding.btToastTop, (v) ->

        {
            WindowUtils.showToastPopView(MyApplication.sApp);
        });
    }

    @Override
    protected int getViewModelId() {
        return -1;
    }

    @Override
    protected <V extends ViewModel> V getViewModel() {
        return (V) getAppViewModelProvider().get(PhoneLiveData.class);
    }
}

